package com.handydata.antifraud.ws.redis;

import com.handydata.antifraud.common.constant.AlarmInfoCode;
import com.handydata.antifraud.common.exception.WsInvokeException;
import com.handydata.antifraud.to.dap.BankBeanCountReqTO;
import com.handydata.antifraud.to.dap.BankBeanCountResTO;
import com.handydata.antifraud.ws.AbstractWsService;
import com.handydata.antifraud.ws.WsInvocationService;
import com.handydata.common.util.RedisUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;


/**
 * 统计上行下发的数据，返回给规则引擎用
 */
@Service
public class BankBeanCountImpl extends AbstractWsService<BankBeanCountReqTO, BankBeanCountResTO>
	implements WsInvocationService<BankBeanCountReqTO, BankBeanCountResTO> {

	private static final Logger logger = LoggerFactory.getLogger(BankBeanCountImpl.class);

	@Autowired
	private RedisUtil redisUtil;
	/**
	 * @see com.handydata.antifraud.ws.AbstractWsService#register()
	 */
	@PostConstruct
	@Override
	public void register() {
		registerToWsServiceFactory(this, BankBeanCountReqTO.class);
	}

	@Override
	public BankBeanCountResTO query(BankBeanCountReqTO req) throws WsInvokeException {

		BankBeanCountResTO res=new BankBeanCountResTO();
		try{
			if(req.getIdCard()!=null&&req.getIdCard().length()>0){

				if(req.getNterDeviceIdCode()!=null&&req.getNterDeviceIdCode().length()>0){
					Long count=redisUtil.incr(AlarmInfoCode.REDIS_BANK_COUNT_KEY+"_"+req.getNterDeviceIdCode()+"_"+req.getIdCard(),1);
					res.setImei_id_card(count.intValue());
				}
				if(req.getDeviceNic()!=null&&req.getDeviceNic().length()>0){
					Long count=redisUtil.incr(AlarmInfoCode.REDIS_BANK_COUNT_KEY+"_"+req.getDeviceNic()+"_"+req.getIdCard(),1);
					res.setMac_id_card(count.intValue());
				}
				if(req.getDeviceIdCode()!=null&&req.getDeviceIdCode().length()>0){
					Long count=redisUtil.incr(AlarmInfoCode.REDIS_BANK_COUNT_KEY+"_"+req.getDeviceIdCode()+"_"+req.getIdCard(),1);
					res.setUuid_id_card(count.intValue());
				}
			}
			if(req.getNterDeviceIdCode()!=null&&req.getNterDeviceIdCode().length()>0
					&&req.getNterDeviceIdentityCode()!=null&&req.getNterDeviceIdentityCode().length()>0){
				Long count=redisUtil.incr(AlarmInfoCode.REDIS_BANK_COUNT_KEY+"_"+req.getNterDeviceIdCode()+"_"+req.getNterDeviceIdentityCode(),1);
				res.setImei_imsi(count.intValue());//还有imsi_imei这不一回事吗
				res.setImsi_imei(count.intValue());
			}
			res.setResCode("0000");
			res.setResMsg("统计成功");
			return res;
		}catch (Exception ex){
			logger.error("统计上行数据出错{}",req,ex);//该抛异常还是返回错误代码？
		}
		return res;
	}
}
